// import axios from "axios";
import store from "../store";
import router from "../router";
import { Toast, Dialog} from 'vant';
// 创建axios实例
console.log(axios,'axios')
const service = axios.create({
  baseURL: process.env.VUE_APP_URL, // api 的 VUE_APP_URL
  timeout: 50000 // 请求超时时间(因为需要调试后台,所以设置得比较大)
});
var isAlter=false
// request拦截器,在请求之前做一些处理
service.interceptors.request.use(
  config => {
	if(config.url.indexOf('/api/med/index') != -1||config.url.indexOf('/api/med/pay_receipt') != -1){
		isAlter=true
	}
	 
    // if (store.state.token) {
    //   // 给请求头添加laohu-token
    //   config.headers["Authorization"] = store.state.token||'or7By6KFvYMZTICvfzIJ1Zh_-T5Q';
    // }
	// 0a59856aor7By6KFvYMZTICvfzIJ1Zh_-T5Q9a77125b1e34
	config.headers["Authorization"] = store.state.token||'0609d99dor7By6MLOwrdnC5joM8fWUFda_pY0fadd59f259e'; 
	
	
	
	
	
    return config;
  },
  error => { 
    console.log(error); // for debug
    Promise.reject(error);
  }
);

// response 拦截器,数据返回后进行一些处理
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
	console.log(response,'response')
    const res = response.data;
	
    if (res.ack_info&&res.ack_info.exe_status == "A" ||res.ack_info&&res.ack_info.Authorization||res.ack_info&&res.ack_info.exe_status!='E'||res.code==0) {
		console.log("0009999=------")
      return res;
    }else if(res.code==200){
		 return res;
	} else {
		var str=res.ack_info.err_msg
		if(isAlter){
			Dialog.alert({
			  type:'danger',
			  title:'错误提示',
			  message:str
			}).then(() => {
			  
			});
			return res;
		}else{
			Toast(res.ack_info.err_msg)
		}
		
       
    }
  },
  error => {
	 console.log(error.response.status,'error.response.status----')
	 if(error.response.status){
		const status = error.response.status
		if(status == 401){
			console.log('token失效')
			router.push("/login"); 
		}
	 } 
     Promise.reject("网络异常");
  }
);
export default service;
